﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeManageService;

namespace WeManageForm
{
    public partial class RegisterForm : Form
    {

        public Point mouseDownPoint;

        public RegisterForm()
        {
            InitializeComponent();
            
        }

        UserManager userManager = new UserManager();

        private void BtnRegister_Click(object sender, EventArgs e)
        {
            if (TxtUserName.Text == "") LblAlertName.Text = "用户名为空！";
            else if (TxtPassword.Text == "")
            {
                LblAlertPassword.Text = "密码为空！";
            }
            else if (TxtPassword.Text == TxtConfirmPassword.Text)
            {
                if (userManager.GetUser(TxtUserName.Text) != null)
                {
                    MessageBox.Show("用户已存在");
                }
                else
                {
                    User user = new User(TxtUserName.Text, TxtPassword.Text);
                    userManager.Register(ref user);
                    MessageBox.Show("注册成功");
                    this.Close();
                }
            }
            //else if () 如果数据库里有这个用户名了注册失败
        }

        private void TxtConfirmPassword_TextChanged(object sender, EventArgs e)
        {
            if(TxtPassword.Text == "" || TxtConfirmPassword.Text == TxtPassword.Text)
            {
                LblAlertConfirm.Text = "";
            }
            else
            {
                LblAlertConfirm.Text = "两次密码不相同！";
            }
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TxtUserName_TextChanged(object sender, EventArgs e)
        {
            LblAlertName.Text = "";
            LblAlertPassword.Text = "";
        }

        private void TxtPassword_TextChanged(object sender, EventArgs e)
        {
            LblAlertName.Text = "";
            LblAlertPassword.Text = "";
            if(TxtPassword.Text == "" || TxtConfirmPassword.Text == TxtPassword.Text)
            {
                LblAlertConfirm.Text = "";
            }
        }

        private void RegisterForm_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDownPoint = new Point(e.X, e.Y);
        }

        private void RegisterForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mouseDownPoint.X,
                    this.Location.Y + e.Y - mouseDownPoint.Y);
            }
        }

        /*
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr LoadLibrary(string lpFileName);
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams prams = base.CreateParams;
                if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
                {
                    prams.ExStyle |= 0x020; // transparent 
                    prams.ClassName = "RICHEDIT50W";
                }
                return prams;
            }
        */
    }
}
